home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / oleo-1_4.lha / oleo-1.4 / stub.c < prev    next >
C/C++ Source or Header  |  1993-03-30  |  7KB  |  305 lines

  1. /*    Copyright (C) 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17.  
  18.  
  19. /* This is a collection of stubs that are used to call interactive functions.
  20.  * Their responsability is to extract arguments from a command_frame as
  21.  * constructed by the function COMMAND_LOOP.
  22.  */
  23.  
  24. #include "global.h"
  25. #include "cmd.h"
  26. #include "stub.h"
  27.  
  28.  
  29.  
  30.  
  31. #ifdef __STDC__
  32. static void
  33. find_args (struct command_arg ** argv_out, int argc, struct command_frame * frame)
  34. #else
  35. static void
  36. find_args (argv_out, argc, frame)
  37.      struct command_arg ** argv_out;
  38.      int argc;
  39.      struct command_frame * frame;
  40. #endif
  41. {
  42.   int found = 0;
  43.   int pos = 0;
  44.   while (found < argc)
  45.     {
  46.       if (frame->argv[pos].style->representation != cmd_none)
  47.     argv_out[found++] = &frame->argv[pos];
  48.       ++pos;
  49.     }
  50. }
  51.  
  52.  
  53. /* These macros are invoked in stubs.h and are used to define
  54.  * the stub functions.  Later, these macros will be redifined 
  55.  * an used to build a table of stub functions.
  56.  */
  57.  
  58. #ifdef __STDC__
  59. #define STUB1(STR,NAME, PRE, VAL, TYPE) \
  60. static void \
  61. NAME (frame) \
  62.      struct command_frame * frame; \
  63. { \
  64.   struct command_arg * argv; \
  65.   find_args (&argv, 1, frame); \
  66.   ((void (*) (TYPE)) frame->cmd->func_func) (PRE argv->val.VAL); \
  67. }
  68. #else
  69. #define STUB1(STR,NAME, PRE, VAL, TYPE) \
  70. static void \
  71. NAME (frame) \
  72.      struct command_frame * frame; \
  73. { \
  74.   int x; \
  75.   struct command_arg * argv; \
  76.   find_args (&argv, 1, frame); \
  77.   frame->cmd->func_func (PRE argv->val.VAL); \
  78. }
  79. #endif
  80.  
  81. #ifdef __STDC__
  82. #define STUB2(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2) \
  83. static void \
  84. NAME (frame) \
  85.      struct command_frame * frame; \
  86. { \
  87.   struct command_arg * argv[2]; \
  88.   find_args (argv, 2, frame); \
  89.   ((void (*) (TYPE1, TYPE2)) frame->cmd->func_func) \
  90.     (PRE1 argv[0]->val.VAL1,  \
  91.      PRE2 argv[1]->val.VAL2); \
  92. }
  93. #else
  94. #define STUB2(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2) \
  95. static void \
  96. NAME (frame) \
  97.      struct command_frame * frame; \
  98. { \
  99.   struct command_arg * argv[2]; \
  100.   find_args (argv, 2, frame); \
  101.   frame->cmd->func_func \
  102.     (PRE1 argv[0]->val.VAL1,  \
  103.      PRE2 argv[1]->val.VAL2); \
  104. }
  105. #endif 
  106.  
  107.  
  108. #ifdef __STDC__
  109. #define STUB3(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3) \
  110. static void \
  111. NAME (frame) \
  112.      struct command_frame * frame; \
  113. { \
  114.   struct command_arg * argv[3]; \
  115.   find_args (argv, 3, frame); \
  116.   frame->cmd->func_func \
  117.     (PRE1 argv[0]->val.VAL1, \
  118.      PRE2 argv[1]->val.VAL2, \
  119.      PRE3 argv[2]->val.VAL3); \
  120. }
  121. #else
  122. #define STUB3(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3) \
  123. static void \
  124. NAME (frame) \
  125.      struct command_frame * frame; \
  126. { \
  127.   struct command_arg * argv[3]; \
  128.   find_args (argv, 3, frame); \
  129.   frame->cmd->func_func \
  130.     (PRE1 argv[0]->val.VAL1, \
  131.      PRE2 argv[1]->val.VAL2, \
  132.      PRE3 argv[2]->val.VAL3); \
  133. }
  134. #endif
  135.  
  136. #ifdef __STDC__
  137. #define STUB4(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3,PRE4,VAL4,TYPE4) \
  138. static void \
  139. NAME (frame) \
  140.      struct command_frame * frame; \
  141. { \
  142.   struct command_arg * argv[4]; \
  143.   find_args (argv, 4, frame); \
  144.   frame->cmd->func_func \
  145.     (PRE1 argv[0]->val.VAL1, \
  146.      PRE2 argv[1]->val.VAL2, \
  147.      PRE3 argv[2]->val.VAL3, \
  148.      PRE4 argv[3]->val.VAL4); \
  149. }
  150. #else
  151. #define STUB4(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3,PRE4,VAL4,TYPE4) \
  152. static void \
  153. NAME (frame) \
  154.      struct command_frame * frame; \
  155. { \
  156.   struct command_arg * argv[4]; \
  157.   find_args (argv, 4, frame); \
  158.   frame->cmd->func_func \
  159.     (PRE1 argv[0]->val.VAL1, \
  160.      PRE2 argv[1]->val.VAL2, \
  161.      PRE3 argv[2]->val.VAL3, \
  162.      PRE4 argv[3]->val.VAL4); \
  163. }
  164. #endif
  165.  
  166. #ifdef __STDC__
  167. #define STUB5(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3,PRE4,VAL4,TYPE4,PRE5,VAL5,TYPE5) \
  168. static void \
  169. NAME (frame) \
  170.      struct command_frame * frame; \
  171. { \
  172.   struct command_arg * argv[5]; \
  173.   find_args (argv, 5, frame); \
  174.   frame->cmd->func_func \
  175.     (PRE1 argv[0]->val.VAL1, \
  176.      PRE2 argv[1]->val.VAL2, \
  177.      PRE3 argv[2]->val.VAL3, \
  178.      PRE4 argv[3]->val.VAL4, \
  179.      PRE5 argv[4]->val.VAL5); \
  180. }
  181. #else
  182. #define STUB5(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3,PRE4,VAL4,TYPE4,PRE5,VAL5,TYPE5) \
  183. static void \
  184. NAME (frame) \
  185.           struct command_frame * frame; \
  186. { \
  187.   struct command_arg * argv[5]; \
  188.   find_args (argv, 5, frame); \
  189.   frame->cmd->func_func \
  190.     (PRE1 argv[0]->val.VAL1, \
  191.      PRE2 argv[1]->val.VAL2, \
  192.      PRE3 argv[2]->val.VAL3, \
  193.      PRE4 argv[3]->val.VAL4, \
  194.      PRE5 argv[4]->val.VAL5); \
  195. }
  196. #endif
  197.  
  198. /* This contains the list of stub functions. */
  199.  
  200. #include "stubs.h"
  201.  
  202. /* There is only one `STUB0' so we needn't bother with a macro. */
  203.  
  204. #ifdef __STDC__
  205. static void
  206. stub_void (struct command_frame * frame)
  207. #else
  208. static void
  209. stub_void (frame)
  210.      struct command_frame * frame;
  211. #endif
  212. {
  213.   frame->cmd->func_func ();
  214. }
  215.  
  216. /* For define_usr_fmt */
  217. static void
  218. stub_isssssssss (frame)
  219.      struct command_frame * frame;
  220. {
  221.   struct command_arg * argv[10];
  222.   find_args (argv, 10, frame);
  223.   frame->cmd->func_func (argv[0]->val.integer,
  224.              argv[1]->val.string,
  225.              argv[2]->val.string,
  226.              argv[3]->val.string,
  227.              argv[4]->val.string,
  228.              argv[5]->val.string,
  229.              argv[6]->val.string,
  230.              argv[7]->val.string,
  231.              argv[8]->val.string,
  232.              argv[9]->val.string);
  233. }
  234.  
  235.  
  236.  
  237.  
  238.  
  239. /* Single character type-codes denote the types of arguments.  A string
  240.  * of type-codes maps to a stub function (hopefully).
  241.  */
  242. struct cmd_stub
  243. {
  244.   char * type;
  245.   cmd_invoker stub;
  246. };
  247.  
  248. #undef STUB1
  249. #undef STUB2
  250. #undef STUB3
  251. #undef STUB4
  252. #undef STUB5
  253.  
  254. #define STUB1(STR,NAME,PRE1,VAL1,TYPE1) { STR, NAME },
  255. #define STUB2(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2)  { STR, NAME },
  256. #define STUB3(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3) \
  257.     { STR, NAME },
  258. #define STUB4(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3,PRE4,VAL4,TYPE4) \
  259.     { STR, NAME },
  260. #define STUB5(STR,NAME,PRE1,VAL1,TYPE1,PRE2,VAL2,TYPE2,PRE3,VAL3,TYPE3,PRE4,VAL4,TYPE4,PRE5,VAL5,TYPE5) \
  261.     { STR, NAME },
  262.  
  263. static struct cmd_stub the_stubs[] =
  264. {
  265.   { "", stub_void },
  266. #include "stubs.h"
  267.   { "isssssssss", stub_isssssssss },
  268.   { 0, 0 }
  269. };
  270.  
  271.  
  272.  
  273. /* This looks at the arguments built for the current command and 
  274.  * finds the right stub.
  275.  */
  276. #ifdef __STDC__
  277. cmd_invoker
  278. find_stub (void)
  279. #else
  280. cmd_invoker
  281. find_stub ()
  282. #endif
  283. {
  284.   char type_buf[100];
  285.  
  286.   /* Figure out a name for the stub we want. */
  287.   {
  288.     int x, bufpos;
  289.     for (x = 0, bufpos = 0; x < cmd_argc; ++x)
  290.       if (the_cmd_frame->argv[x].style->representation != cmd_none)
  291.     type_buf[bufpos++] = the_cmd_frame->argv[x].style->representation;
  292.     type_buf[bufpos] = '\0';
  293.   }
  294.  
  295.   /* Look for the stub. */
  296.   {
  297.     int x;
  298.     for (x = 0; the_stubs[x].type; ++x)
  299.       if (!stricmp (the_stubs[x].type, type_buf))
  300.     break;
  301.     return the_stubs[x].stub;
  302.   }
  303. }
  304.  
  305.